package org.dromara.pay.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfig {

    private WxPayProperties properties;

    /**
     * 与微信小程序有关的总service
     *
     * @return WxMaService
     */
    @Bean
    public WxMaService wxMaService() {

        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(this.properties.getMinAppId());
        config.setSecret(this.properties.getAppSecret());
        /*config.setToken(cof.getToken());
        config.setAesKey(cof.getAesKey());
        config.setMsgDataFormat(cof.getMsgDataFormat());*/
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }

    @Bean(name = "wxMiniService")
    @ConditionalOnMissingBean
    public WxPayService wxMiniService() {
        com.github.binarywang.wxpay.config.WxPayConfig payConfig = new com.github.binarywang.wxpay.config.WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull("wx502a81693987736b"));
        payConfig.setMchId(StringUtils.trimToNull("1695534687"));
        payConfig.setMchKey(StringUtils.trimToNull("7f2443de496e69c6e41aa25347c88368"));

        payConfig.setSubAppId(StringUtils.trimToNull("wx65757cb9be71f395"));
        payConfig.setSubMchId(StringUtils.trimToNull("1720612334"));

        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getPlatformCertPath()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    @Bean(name = "wxAppService")
    @ConditionalOnMissingBean
    public WxPayService wxAppService() {
        com.github.binarywang.wxpay.config.WxPayConfig payConfig = new com.github.binarywang.wxpay.config.WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getPlatformCertPath()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getAppNotifyUrl()));

        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
